<?php
/*
 * Created on Oct 25, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
date_default_timezone_set("Asia/Shanghai");

function multiwordbreak($str,$maxlen = 33){
	$len = mb_strlen($str, 'UTF-8');
	$ch = '';
	if($len<=$maxlen) return $str;
	for ($i=0; $i < $len; $i++){
		if($i%$maxlen==0){
			  $ch .= mb_substr($str, $i, $maxlen, 'UTF-8')."<br />";
		}
    }
    return $ch;
}
function break_word($matches){

	return $matches[1].multiwordbreak($matches[3],$matches[2]);
}

require_once('html2fpdf.php');
$pdf = new HTML2FPDF("P","mm");

$data = isset($_POST['data'])?$_POST['data']:'';
echo "data=".$data;


//连接数据库
$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_query("SET NAMES 'UTF8'") or die(mysql_error);
mysql_select_db("sq_shoppers",$con);

$sql = "select * from answer where rq_id=954 and a_id=468";
$result = mysql_query($sql, $con) or die(mysql_error());
$row = mysql_fetch_assoc($result);

echo $row['ans_answer3'];
//$s = str_replace("\r\n","&lt;br /&gt;",$row['ans_answer3']);
//echo "<br/>".$s;

$content = "<table border=2>" .
		"<tr><th>title1</th><th>title2</th></tr>" .
		"<tr><td>row1row1row1row1row1row1row1</td><td>my pdf testmy pdf testmy pdf testmy pdf testmy pdf testmy pdf testmy pdf testmy pdf testmy pdf testmy pdf testmy pdf testmy pdf testmy pdf testmy pdf test</td></tr>" .
		"<tr><td>row2row2row2row2row2row2row2</td><td>这是我的PDF测试. \\r\\n这是我的PDF测试</td></tr>" .
		"</table>";

$strContent = '';
	//$c = preg_replace_callback('/(.*?)<span\s*id="br_(\d+)">(.*?)<\/span>(.*?)/ism',"break_word",$row['ans_answer3']);
	$c = preg_replace_callback('/(.*?)<span\s*id="br_(\d+)">(.*?)<\/span>(.*?)/ism',"break_word",$data);


	if(strpos($c,'border="0"')!==false) $c = str_replace('border="0"','border="1"',$c);


	$strContent = stripslashes($c);

$strContent = str_replace("\r\n","&lt;br /&gt;",$strContent);
//$strContent = str_replace("(","&#40;",$strContent);
//$strContent = str_replace("）","&#41;",$strContent);
echo "<br/>".$strContent."<br/>";


//连接数据库
//$con = mysql_connect("localhost","root","") or die(mysql_error());
//mysql_query("SET NAMES 'UTF8'") or die(mysql_error);
//mysql_select_db("sq_shoppers",$con);
//
//$sql = "select * from answer where rq_id=954 and a_id=468";
//$result = mysql_query($sql, $con) or die(mysql_error());
//$row = mysql_fetch_assoc($result);
//
//echo $row['ans_answer3'];
//$s = str_replace("\r\n","&lt;br /&gt;",$row['ans_answer3']);
//echo "<br/>".$s;


//$strContent = str_replace("<br />","\r\n",$strContent);
//echo "<br/>after:".$strContent;

$pdf->SetMargins(10,20,-1);//设置左右及上边距
$pdf->AddGBFont();
$pdf->AddPage();

$pdf->WriteHTML($strContent);
//$pdf->WriteHTML($strContent);
$pdf->Output('../tmp/test.pdf');
?>
